package com.mentaleye.controller;

import com.mentaleye.bean.GroupBean;
import com.mentaleye.bean.GroupPersonBean;
import com.mentaleye.biz.DictionaryBiz;
import com.mentaleye.biz.GroupBiz;
import com.mentaleye.biz.GroupPersonBiz;
import com.mentaleye.enums.DictionaryEnums;
import com.mentaleye.util.CommonPage;
import com.mentaleye.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Rock on 2017/5/10.
 */
@Controller
@RequestMapping("/groupPerson")
public class GroupPersonController {

    @Autowired private GroupPersonBiz groupPersonBiz;
    @Autowired private GroupBiz groupBiz;
    @Autowired private DictionaryBiz dictionaryBiz;

    @RequestMapping("/listData")
    @ResponseBody
    public String getData(CommonPage commonPage){
        groupPersonBiz.queryByCommpage(commonPage);
        return commonPage.toSHowPage();
    }

    @RequestMapping("/delete")
    @ResponseBody
    public CommonResult remove(@RequestParam(value = "personIds") int[] personIds){
        List<Integer> ids = new ArrayList<Integer>();
        for (int i = 0; i < personIds.length; i++) {
            ids.add(personIds[i]);
        }
        return groupPersonBiz.removeList(ids);
    }

    @RequestMapping("/edit")
    public ModelAndView edit(@RequestParam(value = "id") Integer id){
        ModelAndView view = new ModelAndView("ea/group/groupPersonEdit");
        Map<String, Object> groupPerson = groupPersonBiz.queryByPersonId(id);
        Object parentId = groupPerson.get("parent_id");
        List<GroupBean> groups = groupBiz.queryByParentId(Integer.valueOf(parentId.toString()));
        Map<Integer, String> roles = dictionaryBiz.queryDictionaryByParent(DictionaryEnums.GROUP_PERSON_ROLE.getCode());
        view.addObject("edit",true);
        view.addObject("roleList",roles);
        view.addObject("groupList",groups);
        view.addObject("beanMap",groupPerson);
        return view;
    }

    @RequestMapping("/add")
    public ModelAndView add(@RequestParam("id")Integer classId,@RequestParam("groupId")Integer groupId){
        ModelAndView view = new ModelAndView("ea/group/groupPersonEdit");
        List<Map<String, Object>> stuList = groupPersonBiz.queryNotInClass(classId);
        List<GroupBean> groups = groupBiz.queryByParentId(classId);
        Map<Integer, String> roles = dictionaryBiz.queryDictionaryByParent(DictionaryEnums.GROUP_PERSON_ROLE.getCode());
        view.addObject("edit",false);
        view.addObject("groupId",groupId);
        view.addObject("roleList",roles);
        view.addObject("groupList",groups);
        view.addObject("stuList",stuList);
        return view;
    }

    @RequestMapping("/save")
    @ResponseBody
    public CommonResult save(GroupPersonBean bean){
        if(bean.getSid() != null){
            bean.setPersonId(bean.getSid());
            return groupPersonBiz.add(bean);
        }
        return groupPersonBiz.edit(bean);
    }
}
